<template>
  <div>
    <div class="banner" :style="cover">
      <h1 class="banner-title">个人中心</h1>
    </div>
    <v-card class="blog-container">
    <el-menu :default-active="activeIndex" class="el-menu-demo" mode="horizontal"  @select="handleSelect">
      <el-menu-item index="1">基本信息</el-menu-item>
      <el-menu-item index="2">我的订单</el-menu-item>
    </el-menu>
      <div v-if="activeIndex==1">
        <div>
        <span class="info-title">基本信息</span>
      </div>
      <v-row class="info-wrapper">
        <v-col md="3" cols="12">
          <button id="pick-avatar">
            <v-avatar size="140">
              <img :src="this.$store.state.avatar" />
            </v-avatar>
          </button>
          <avatar-cropper
            @uploaded="uploadAvatar"
            trigger="#pick-avatar"
            upload-url="/api/users/avatar"
          />
        </v-col>
        <v-col md="7" cols="12">
          <v-text-field
            v-model="userInfo.nickname"
            label="昵称"
            placeholder="请输入您的昵称"
          />
          <v-text-field
            v-model="userInfo.webSite"
            class="mt-7"
            label="个人网站"
            placeholder="http://你的网址"
          />
          <v-text-field
            v-model="userInfo.intro"
            class="mt-7"
            label="账户余额"
            disabled
          />
          <div v-if="loginType != 0" class="mt-7 binding">
            <v-text-field
              disabled
              v-model="email"
              label="邮箱号"
              placeholder="请绑定邮箱"
            />
            <v-btn v-if="email" text small @click="openEmailModel">
              修改绑定
            </v-btn>
            <v-btn v-else text small @click="openEmailModel">
              绑定邮箱
            </v-btn>
          </div>
          <v-btn @click="updataUserInfo" outlined class="mt-5">修改</v-btn>
        </v-col>
      </v-row>
      </div>
      <div v-else>
        <div>
          <span class="info-title">我的订单</span>
        </div>
        <el-button icon="el-icon-thumb" @click="handlePay" size="small">充值</el-button>
        <el-table
            :data="orderList"
            style="width: 100%">
          <el-table-column
              prop="name"
              label="课程名"
              >
          </el-table-column>
          <el-table-column
              prop="createTime"
              label="购买时间"
              align="center"
          >
            <template slot-scope="scope">
              <i class="el-icon-time" style="margin-right:5px"/>
              {{ scope.row.createTime | date }}
            </template>
          </el-table-column>
          <el-table-column
              prop="money"
              label="价格">
          </el-table-column>
        </el-table>
        <el-pagination
            current-page="1"
            :page-sizes="[10, 20, 30, 40]"
            :page-size="10"
            layout="total, sizes, prev, pager, next, jumper"
            :total="orderTotal">
        </el-pagination>
        <el-dialog :visible.sync="showPay" width="30%">
          <el-form label-width="80px" size="medium">
          <el-form-item label="充值金额">
            <el-input v-model="userInfo.pay" style="width:250px" />
          </el-form-item>
          </el-form>
          <div slot="footer">
            <el-button @click="showPay = false">取 消</el-button>
            <el-button type="primary" @click="updataUserInfo">
              确 定
            </el-button>
          </div>
        </el-dialog>
      </div>
    </v-card>
  </div>
</template>

<script>
import AvatarCropper from "vue-avatar-cropper";
export default {
  components: { AvatarCropper },
  data: function() {
    return {
      userInfo: {
        nickname: this.$store.state.nickname,
        intro: this.$store.state.intro,
        webSite: this.$store.state.webSite,
        loginType: this.$store.state.loginType
      },
      activeIndex: '1',
      orderList:[],
      orderTotal:0,
      showPay: false
    };
  },
  created() {
    this.getMyOrder()
  },
  methods: {
    //获取个人订单
    getMyOrder(){
      this.axios.get("/api/courseRecord/list").then(({ data }) => {
       this.orderList = data.data.recordList
        this.orderTotal = data.data.count
        this.orderList.forEach(i=>{
          this.axios.get("/api/courses/listDetails", {
            params: {
              id: i.courseId,
            }
          }).then(({data}) => {
            i.name=data.data.name
          });
        })
      });
    },
    handlePay(){
      delete this.userInfo.pay
      this.showPay=true
    },
    updataUserInfo() {
      if (this.userInfo.pay!==undefined){
        this.userInfo.intro= parseFloat(this.userInfo.pay)+parseFloat(this.userInfo.intro)
      }
      this.axios.put("/api/users/info", this.userInfo).then(({ data }) => {
        if (data.flag) {
          this.$store.commit("updateUserInfo", this.userInfo);
          this.$toast({ type: "success", message: "修改成功" });
          this.showPay = false
        } else {
          this.$toast({ type: "error", message: data.message });
        }
      });
    },
    uploadAvatar(data) {
      if (data.flag) {
        this.$store.commit("updateAvatar", data.data);
        this.$toast({ type: "success", message: "上传成功" });
      } else {
        this.$toast({ type: "error", message: data.message });
      }
    },
    openEmailModel() {
      this.$store.state.emailFlag = true;
    },
    handleSelect(key, keyPath) {
      console.log(key, keyPath);
      this.activeIndex = key

    }
  },
  computed: {
    email() {
      return this.$store.state.email;
    },
    loginType() {
      return this.$store.state.loginType;
    },
    cover() {
      var cover = "";
      this.$store.state.blogInfo.pageList.forEach(item => {
        if (item.pageLabel == "user") {
          cover = item.pageCover;
        }
      });
      return "background: url(" + cover + ") center center / cover no-repeat";
    }
  }
};
</script>

<style scoped>
.info-title {
  font-size: 1.25rem;
  font-weight: bold;
}
.info-wrapper {
  margin-top: 1rem;
  display: flex;
  align-items: center;
  justify-content: center;
}
#pick-avatar {
  outline: none;
}
.binding {
  display: flex;
  align-items: center;
}
</style>
